/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package achi.community.irla.beans;

import java.awt.event.ActionEvent;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Locale;
import javax.faces.application.Application;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import java.io.Serializable;
import javax.faces.application.ViewHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIViewRoot;
import javax.faces.event.ValueChangeEvent;

/**
 *
 * @author Rignas
 */
@ManagedBean
@SessionScoped
public class LocaleBean implements Serializable {

    private Locale locale;

    public Locale getLocale() {
        if (locale == null) {
            locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
        }
        return locale;
    }

    public void setLocale(Locale locale) {
        this.locale = locale;
    }

    public void changeLocale(ValueChangeEvent event) {
       // FacesContext context = FacesContext.getCurrentInstance();
       // context.renderResponse();

        String selected = event.getNewValue().toString();
        setLocale(new Locale(selected));
        update();
    }

    public void update() {
        FacesContext context = FacesContext.getCurrentInstance();
        String viewId = context.getViewRoot().getViewId();
        ViewHandler handler = context.getApplication().getViewHandler();
        UIViewRoot root = handler.createView(context, viewId);
        root.setViewId(viewId);
        context.setViewRoot(root);
    }
    /**

    public SelectItem[] getLocales() {
    ArrayList items = new ArrayList();
    Application application = FacesContext.getCurrentInstance().getApplication();
    Iterator supportedLocales = application.getSupportedLocales();
    while (supportedLocales.hasNext()) {
    Locale locale = (Locale) supportedLocales.next();
    items.add(new SelectItem(locale.toString(), locale.getDisplayName()));
    }
    SelectItem[] locales = new SelectItem[items.size()];
    items.toArray(locales);
    SelectItem a = new SelectItem("svenska");
    SelectItem b = new SelectItem("engelska");
    SelectItem[] locales2 = {a, b};
    return locales2;
    }
    

    public String getSelectedLocale() {
    return getLocale().toString();
    }


    

    public void setSelectedLocale(String localeString) {
    Application application = FacesContext.getCurrentInstance().getApplication();
    Iterator supportedLocales = application.getSupportedLocales();
    while (supportedLocales.hasNext()) {
    Locale locale = (Locale) supportedLocales.next();
    if (locale.toString().equals(localeString)) {
    this.locale = locale;
    }
    }
    }


    public void changeLocaleSV() {
    setLocale(new Locale("sv"));
    //HtmlCommandButton b = (HtmlCommandButton) evt.getSource();
    //Long id = Long.parseLong(b.getLabel());
    //db.removeMessage(id);
    update();
    }

    public void changeLocaleEN() {
    setLocale(new Locale("en"));
    update();

    //HtmlCommandButton b = (HtmlCommandButton) evt.getSource();
    //Long id = Long.parseLong(b.getLabel());
    //db.removeMessage(id);
    }
    

     */
    /**
    public SelectItem[] getComponentItems() {
    SelectItem[] componentItems = {new SelectItem("Svenska"), new SelectItem("Engelska")};
    return componentItems;
    }
     */
}
